package org.leo.dm.example.writer;

import java.util.HashMap;
import java.util.Map;

import org.leo.dm.common.bean.Constants;
import org.leo.dm.common.bean.StepResultBean;
import org.leo.dm.component.writer.Writer;
import org.leo.dm.util.ObjectUtil;
import org.leo.dm.util.SaxWriteXmlUtil;
import org.leo.dm.util.ToUtil;
import org.leo.dm.util.ZipBase64Util;

public class SaxXmlWriter extends Writer {

	private String destPath;
	private String destZipPath;

	public void setDestPath(String destPath) {
		this.destPath = destPath;
	}

	public void setDestZipPath(String destZipPath) {
		this.destZipPath = destZipPath;
	}

	private Map<String, String> xmlMap;

	public void setXmlMap(Map<String, String> xmlMap) {
		this.xmlMap = xmlMap;
	}

	@Override
	public StepResultBean call() throws Exception {
		StepResultBean resultBean = new StepResultBean();
		try {
			SaxWriteXmlUtil xml = new SaxWriteXmlUtil(destPath, "messages");

			xml.startElement("switch");
			xml.write(this.xmlMap.get("sourcedomain"), "sourcedomain");
			xml.endElement("switch");

			xml.startElement("business");

			xml.startElement("description");
			xml.write(this.xmlMap.get("businesstype"), "businesstype");
			xml.write(this.xmlMap.get("taskid"), "taskid");
			xml.write(this.xmlMap.get("callbackdomainurl"), "callbackdomainurl");
			xml.write(this.xmlMap.get("callbackdomainfun"), "callbackdomainfun");
			xml.write(this.xmlMap.get("callbackdomainns"), "callbackdomainns");
			xml.endElement("description");

			HashMap<Object, Object> mapWriteTemp = null;

			Object[] data = null;

			xml.startElement("data");
			int targetDataNamesLength = 0;
			while (isRunFlag()) {
				data = storage.getData();
				counter++;// 计数器累加

				// 非业务数据
				if (Constants.NONBUSINESSDATA.equals(data[0]) && Constants.QUEUE.equals(data[1]) && Constants.START.equals(data[2])) {
					ToUtil.to("开始接收数据");
					continue;
				} else if (Constants.NONBUSINESSDATA.equals(data[0]) && Constants.SEGMENT.equals(data[1]) && Constants.START.equals(data[2])) {
					targetDataNames = (Object[]) data[5];

					xml.startElement("dataset");
					xml.write(String.valueOf(data[4]), "datasetcode");
					continue;
				} else if (Constants.NONBUSINESSDATA.equals(data[0]) && Constants.SEGMENT.equals(data[1]) && Constants.END.equals(data[2])) {
					xml.endElement("dataset");
					continue;
				} else if (Constants.NONBUSINESSDATA.equals(data[0]) && Constants.QUEUE.equals(data[1]) && Constants.END.equals(data[2])) {
					setRunFlag(false);
					Map<String, Object> callBackMap = new HashMap<String, Object>();
					callBackMap.put("counter", counter);
					callback.handle(callBackMap);
					continue;
				} else if (Constants.NONBUSINESSDATA.equals(data[0]) && Constants.ERROR.equals(data[1])) {
					setRunFlag(false);
					continue;
				}

				mapWriteTemp = new HashMap<Object, Object>();

				targetDataNamesLength = targetDataNames.length;
				for (int i = 0; i < targetDataNamesLength; i++) {
					mapWriteTemp.put(targetDataNames[i], ObjectUtil.nullChange(data[i]));
				}

				xml.write(mapWriteTemp, "detail");

			}
			xml.endElement("data");
			xml.endElement("business");
			xml.end();

			if (destZipPath != null) {
				new ZipBase64Util().encodeF(destPath, destZipPath);
			}

			ToUtil.to("退出数据写入执行");
			resultBean.setCode("1");
			resultBean.setName("运行正常完成");
			resultBean.setSuccessCount(counter);
		} catch (Exception e) {
			e.printStackTrace();
			resultBean.setCode("-1");
			resultBean.setName("运行发生异常");
			resultBean.setErrorDesc(e.getMessage());
		} finally {
			counter = 0;// 计数器清零
		}

		return resultBean;
	}

}
